(function(window) {

var $effect  = null;
var $loading = null;
var effect   = {};
var efunc    = function() {};

effect.onCreate = function() {
	$effect  = pcc2.$c.children('.effect');
	$loading = pcc2.$space.children('.loading');
};
effect.init = function() {
	// home.init();
};

/*------- PAGE TRANSITION EFFECT -------*/
var pt = {};

pt.array = [["pt-page-moveToLeft","pt-page-moveFromRight"],["pt-page-moveToRight","pt-page-moveFromLeft"],["pt-page-moveToTop","pt-page-moveFromBottom"],["pt-page-moveToBottom","pt-page-moveFromTop"],["pt-page-fade","pt-page-moveFromRight pt-page-ontop"],["pt-page-fade","pt-page-moveFromLeft pt-page-ontop"],["pt-page-fade","pt-page-moveFromBottom pt-page-ontop"],["pt-page-fade","pt-page-moveFromTop pt-page-ontop"],["pt-page-moveToLeftFade","pt-page-moveFromRightFade"],["pt-page-moveToRightFade","pt-page-moveFromLeftFade"],["pt-page-moveToTopFade","pt-page-moveFromBottomFade"],["pt-page-moveToBottomFade","pt-page-moveFromTopFade"],["pt-page-moveToLeftEasing pt-page-ontop","pt-page-moveFromRight"],["pt-page-moveToRightEasing pt-page-ontop","pt-page-moveFromLeft"],["pt-page-moveToTopEasing pt-page-ontop","pt-page-moveFromBottom"],["pt-page-moveToBottomEasing pt-page-ontop","pt-page-moveFromTop"],["pt-page-scaleDown","pt-page-moveFromRight pt-page-ontop"],["pt-page-scaleDown","pt-page-moveFromLeft pt-page-ontop"],["pt-page-scaleDown","pt-page-moveFromBottom pt-page-ontop"],["pt-page-scaleDown","pt-page-moveFromTop pt-page-ontop"],["pt-page-scaleDown","pt-page-scaleUpDown pt-page-delay300"],["pt-page-scaleDownUp","pt-page-scaleUp pt-page-delay300"],["pt-page-moveToLeft pt-page-ontop","pt-page-scaleUp"],["pt-page-moveToRight pt-page-ontop","pt-page-scaleUp"],["pt-page-moveToTop pt-page-ontop","pt-page-scaleUp"],["pt-page-moveToBottom pt-page-ontop","pt-page-scaleUp"],["pt-page-scaleDownCenter","pt-page-scaleUpCenter pt-page-delay400"],["pt-page-rotateRightSideFirst","pt-page-moveFromRight pt-page-delay200 pt-page-ontop"],["pt-page-rotateLeftSideFirst","pt-page-moveFromLeft pt-page-delay200 pt-page-ontop"],["pt-page-rotateTopSideFirst","pt-page-moveFromTop pt-page-delay200 pt-page-ontop"],["pt-page-rotateBottomSideFirst","pt-page-moveFromBottom pt-page-delay200 pt-page-ontop"],["pt-page-flipOutRight","pt-page-flipInLeft pt-page-delay500"],["pt-page-flipOutLeft","pt-page-flipInRight pt-page-delay500"],["pt-page-flipOutTop","pt-page-flipInBottom pt-page-delay500"],["pt-page-flipOutBottom","pt-page-flipInTop pt-page-delay500"],["pt-page-rotateFall pt-page-ontop","pt-page-scaleUp"],["pt-page-rotateOutNewspaper","pt-page-rotateInNewspaper pt-page-delay500"],["pt-page-rotatePushLeft","pt-page-moveFromRight"],["pt-page-rotatePushRight","pt-page-moveFromLeft"],["pt-page-rotatePushTop","pt-page-moveFromBottom"],["pt-page-rotatePushBottom","pt-page-moveFromTop"],["pt-page-rotatePushLeft","pt-page-rotatePullRight pt-page-delay180"],["pt-page-rotatePushRight","pt-page-rotatePullLeft pt-page-delay180"],["pt-page-rotatePushTop","pt-page-rotatePullBottom pt-page-delay180"],["pt-page-rotatePushBottom","pt-page-rotatePullTop pt-page-delay180"],["pt-page-rotateFoldLeft","pt-page-moveFromRightFade"],["pt-page-rotateFoldRight","pt-page-moveFromLeftFade"],["pt-page-rotateFoldTop","pt-page-moveFromBottomFade"],["pt-page-rotateFoldBottom","pt-page-moveFromTopFade"],["pt-page-moveToRightFade","pt-page-rotateUnfoldLeft"],["pt-page-moveToLeftFade","pt-page-rotateUnfoldRight"],["pt-page-moveToBottomFade","pt-page-rotateUnfoldTop"],["pt-page-moveToTopFade","pt-page-rotateUnfoldBottom"],["pt-page-rotateRoomLeftOut pt-page-ontop","pt-page-rotateRoomLeftIn"],["pt-page-rotateRoomRightOut pt-page-ontop","pt-page-rotateRoomRightIn"],["pt-page-rotateRoomTopOut pt-page-ontop","pt-page-rotateRoomTopIn"],["pt-page-rotateRoomBottomOut pt-page-ontop","pt-page-rotateRoomBottomIn"],["pt-page-rotateCubeLeftOut pt-page-ontop","pt-page-rotateCubeLeftIn"],["pt-page-rotateCubeRightOut pt-page-ontop","pt-page-rotateCubeRightIn"],["pt-page-rotateCubeTopOut pt-page-ontop","pt-page-rotateCubeTopIn"],["pt-page-rotateCubeBottomOut pt-page-ontop","pt-page-rotateCubeBottomIn"],["pt-page-rotateCarouselLeftOut pt-page-ontop","pt-page-rotateCarouselLeftIn"],["pt-page-rotateCarouselRightOut pt-page-ontop","pt-page-rotateCarouselRightIn"],["pt-page-rotateCarouselTopOut pt-page-ontop","pt-page-rotateCarouselTopIn"],["pt-page-rotateCarouselBottomOut pt-page-ontop","pt-page-rotateCarouselBottomIn"],["pt-page-rotateSidesOut","pt-page-rotateSidesIn pt-page-delay200"],["pt-page-rotateSlideOut","pt-page-rotateSlideIn"]];

pt.effect = function(oldpage, nowpage, hidecb, showcb) {
	var cb = function() {
		hidecb();
		showcb();
	};

	if(oldpage && nowpage) {
		if(nowpage.$el.find('.e_title').length <= 0) {
			// return;
				
			var animates = [58, 59, 60, 61];
			var animate  = pt.array[_.sample(animates)];

			oldpage.$el.addClass(animate[0]);
			nowpage.$el.addClass(animate[1]);

			nowpage.$el.on('webkitAnimationEnd', function() {
				nowpage.$el.off('webkitAnimationEnd');
				oldpage.$el.removeClass(animate[0]);
				nowpage.$el.removeClass(animate[1]);
			});
		}
	}
	cb && cb();
};

effect.pt = pt.effect;
/*------- PAGE TRANSITION EFFECT -------*/


/*------- OVERLAY EFFECT -------*/
var showE = {};
showE.effect = function($dom, cb) {

};

/*------- OVERLAY EFFECT -------*/


/*------- OVERLAY EFFECT -------*/
var overlay = {};

overlay.effect = function($dom, cb) {
	$effect.show();
	$dom.transition({
		scale:              0.2,
		'background-color': '#dcdcdc'
	}, 0).transition({
		scale:              1,
		'background-color': '#ffffff'
	}, 600, function() {
		$effect.hide();
		cb && cb();
	});
};
overlay.hideEffect = function($dom, cb) {
	$effect.show();

	// $dom.fadeOut(function() {
	// 	$effect.hide();
	// 	cb && cb();
	// });

	$dom.transition({
		scale:              0.2,
		'background-color': '#dcdcdc'
	}, 600).transition({
		scale:              1,
		'background-color': '#ffffff'
	}, 0, function() {
		$effect.hide();
		cb && cb();
	});
};

effect.overlay     = overlay.effect;
effect.hideOverlay = overlay.hideEffect;
/*------- OVERLAY EFFECT -------*/


/*------- Mode PAGE SHOW EFFECT -------*/
var mode = {};
mode.effect = function(cb) {
	var page    = pf.pages[pf.getActivePage()];
	var $page   = page.$el;
	var $header = $page.find('.porsche_model');
	var $center = $page.find('.model_center');
	var $dom    = $header.add($center);

	if($header.length>0 && $center.length>0) {
		$effect.show();
		$dom.transition({
			x: -1500
		}, 0).transition({
			x: 0
		}, function() {
			$effect.hide();
			cb && cb();
		});
	}
};
effect.mode = mode.effect;
/*------- Mode PAGE SHOW EFFECT -------*/

/*------- HOME PAGE SHOW EFFECT -------*/
var home = {
	$dom:   null,
	$title: null,
	$cars:  null
};
home._init = function() {
	var $dom    = home.$dom = pf.pages[pf.getActivePage()].$el;
	home.$title = $dom.find('.e_title');
	home.$cars  = $dom.find('.e_car');
};
home.effect = function(cb) {
	if(config.ondebug('noEffectHome')) {
		return cb && cb();
	}

	home._init();

	var $t = home.$title;
	var $c = home.$cars;
	$effect.show();

	var i = 0;
	var _cb = function() {
		if(++i == ($c.length+1)) {
			$effect.hide();
			cb && cb();
		}
	};

	$t.transition({
		x:      -2000,
	}, 0).transition({
		x:      0,
	}, 1000, 'in', _cb);

	$c.transition({
		scale:   0.2,
		opacity: 0,
		y:       500
	}, 0).each(function(index) {
		$(this).delay(index*600+1000).transition({
			scale:   1,
			opacity: 1,
			y:       0
		}, 1000, 'in', _cb);
	});
};
effect.home = home.effect;
/*------- HOME PAGE SHOW EFFECT -------*/

/*------- LOADING EFFECT -------*/
var _loading = false;
var _loadCB  = null;

var onLoading = function(rate) {
	if(_loading) {
		if(rate >= 1) onLoadEnd();

		rate = Math.round(rate * 100) + ' %';
		$loading.find('.process').text(rate);
	}
};
var onLoadEnd = function() {
	_loading = false;
	window.setTimeout(function() {
		$loading.find('.process').hide();
		$loading.find('.logo').transit({
			'margin-top': 23,
			'opacity':    0.5
		}, 1000, function() {
			_loadCB && _loadCB();
			pcc2.$space.fadeOut($loading.remove);
		});
	}, 700);
};
/**
 * loading effect
 * @param  {Function} cb 回调函数
 * @return {Function}    更新rate函数
 */
effect.loading = function(cb) {
	if(config.ondebug('noPreload')) {
		_loadCB  = cb;
		_loading = false;
		_loadCB && _loadCB();
		pcc2.$space.fadeOut($loading.remove);
		return function() {};
	}
	if(!_loading) {
		_loading = true;
		_loadCB  = cb;
		return onLoading;
	}
};
/*------- LOADING EFFECT -------*/

window.effect = effect;

})(window);